<template>
  <div class="logistics">
    <div class="screen_box">
      <div class="select_box">
        <div class="select_li">
          <el-select v-model="yearData" placeholder="请选择" size="mini" @change="selectYear" value-key="year">
            <el-option v-for="item in yearOption" :key="item.year" :label="item.year + '年'" :value="item"></el-option>
          </el-select>
        </div>
        <div class="select_li">
          <el-select v-model="monthData" placeholder="请选择" size="mini" @change="selectMonth">
            <el-option
              v-for="item in monthOption"
              :key="item.month"
              :label="item.month + '月'"
              :value="item.month"></el-option>
          </el-select>
        </div>
        <div class="select_li">
          <el-date-picker
            size="small"
            v-model="dateVal"
            type="monthrange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="onChangeDate"
            :editable="false"
            :unlink-panels="true"
            format="yyyy-MM"
            value-format="yyyy-MM"></el-date-picker>
        </div>
      </div>
      <div class="operate_box">
        <el-button type="primary" size="small" @click="exportData">导 出</el-button>
      </div>
    </div>
    <div class="info_con">
      <div class="info_left" v-if="userType === '3'">
        <org-list ref="orgList" @onClickChangeClick="onChange"></org-list>
      </div>
      <div class="info_right">
        <div class="info_detail_title">
          <span class="title_name" v-if="timeType === '1'">{{ monthData }}月办公用品领用明细汇总</span>
          <span class="title_name" v-if="timeType === '2'">
            {{ startYear }}年{{ startMonth }}月——{{ endYear }}年{{ endMonth }}月办公用品领用明细汇总
          </span>
        </div>
        <div class="table_con" ref="table_con">
          <vxe-table
            border
            stripe
            class="mytable-scrollbar"
            highlight-hover-row
            show-overflow
            size="medium"
            height="100%"
            :data="tableData"
            align="center"
            v-if="!load">
            <vxe-table-column field="name" title="物品名称" min-width="140"></vxe-table-column>
            <vxe-table-column field="specs" title="规格" min-width="120"></vxe-table-column>
            <vxe-table-column field="applyNumber" title="申请数量" min-width="100"></vxe-table-column>
            <vxe-table-column field="realityNumber" title="实际领取数量" min-width="140"></vxe-table-column>
          </vxe-table>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getStatisticsList, getYearMonthList, exportCollectExcel } from '@apis/api-dailyManage.js'
import orgList from './orgList'
import moment from 'moment'
export default {
  name: 'logisticsStatistics',
  components: { orgList },
  props: {
    userType: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      load: false,
      statusOptions: [
        {
          value: '',
          label: '全部',
        },
        {
          value: '0',
          label: '待审定',
        },
        {
          value: '1',
          label: '审定通过',
        },
        {
          value: '2',
          label: '审定不通过',
        },
      ],
      statusData: '',
      yearOption: [],
      yearData: {},
      monthOption: [],
      monthData: moment().format('M'),
      tableData: [],
      orgId: '',
      userInfo: JSON.parse(localStorage.PSKH_USERINFO),
      dateVal: [],
      timeType: '1',
      startYear: '',
      startMonth: '',
      endYear: '',
      endMonth: '',
    }
  },
  computed: {},
  filters: {},
  watch: {},
  created() {
    this.getYearMonthList()
  },
  mounted() {},
  methods: {
    getData() {
      if (this.yearData.year === '全部') return
      const param = {
        type: this.timeType,
        orgId: this.orgId,
        startYear: this.timeType === '1' ? this.yearData.year : this.startYear,
        startMonth: this.timeType === '1' ? this.monthData : this.startMonth,
        endYear: this.timeType === '1' ? '' : this.endYear,
        endMonth: this.timeType === '1' ? '' : this.endMonth,
      }
      getStatisticsList(param).then(res => {
        this.load = false
        this.tableData = res.data
      })
    },
    selectYear() {
      this.timeType = '1'
      this.monthOption = this.yearData.monthList
      this.monthData = this.monthOption[0].month
      this.dateVal = []
      this.startYear = ''
      this.startMonth = ''
      this.endYear = ''
      this.endMonth = ''
      this.getData()
    },
    selectMonth() {
      this.getData()
    },
    onChange(data) {
      this.orgId = data.orgId
      this.getData()
    },
    getYearMonthList() {
      getYearMonthList({
        type: '0',
      }).then(res => {
        this.yearOption = res.data || []
        for (let i = 0; i < this.yearOption.length; i++) {
          if (this.yearOption[i].choose) {
            this.yearData = this.yearOption[i]
            break
          }
        }
        this.monthOption = this.yearData.monthList
        for (let i = 0; i < this.monthOption.length; i++) {
          if (this.monthOption[i].choose) {
            this.monthData = this.monthOption[i].month
            break
          }
        }
        if (this.yearData.year !== '全部') {
          if (['1', '2'].includes(this.userType)) {
            this.orgId = this.userInfo.loginPostInfo.orgId
          }
          this.getData()
        }
      })
    },
    onChangeDate(e) {
      this.timeType = '2'
      if (e) {
        this.yearData = ''
        this.monthData = ''
        this.monthOption = []
        const arr1 = e[0].split('-')
        const arr2 = e[1].split('-')
        this.startYear = arr1[0]
        this.startMonth = String(Number(arr1[1]))
        this.endYear = arr2[0]
        this.endMonth = String(Number(arr2[1]))
      } else {
        this.timeType = '1'
        this.yearData = this.yearOption[0]
        this.monthOption = this.yearOption[0].monthList
        this.monthData = this.monthOption[0].month
      }
      this.getData()
    },
    exportData() {
      const startYear = this.timeType === '1' ? this.yearData.year : this.startYear
      const startMonth = this.timeType === '1' ? this.monthData : this.startMonth
      const endYear = this.timeType === '1' ? '' : this.endYear
      const endMonth = this.timeType === '1' ? '' : this.endMonth
      const req = `?startYear=${startYear}&startMonth=${startMonth}&endYear=${endYear}&endMonth=${endMonth}&orgId=${this.orgId}&type=${this.timeType}&tenantId=${localStorage.pskh_tenantId}`
      const link = document.createElement('a')
      link.href = exportCollectExcel(req)
      link.target = 'targetName'
      console.log(link.href)
      link.click()
      link.remove()
    },
  },
}
</script>

<style lang="scss" scoped>
.logistics {
  height: 100%;
  .screen_box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    height: 52px;
    border-bottom: 1px solid #efefef;
    // box-shadow: 0 3px 5px 0 rgba(2, 2, 2, 0.1);
    .select_box {
      display: flex;
      align-items: center;
      .select_li {
        display: flex;
        align-items: center;
        margin-right: 20px;
        .el-select {
          width: 150px;
        }
        /deep/.el-input__inner {
          border: 1px solid #dcdfe6;
        }
        /deep/.el-range-separator {
          width: auto;
        }
      }
    }
  }
  .info_con {
    display: flex;
    height: calc(100% - 60px);
    .info_left {
      width: 200px;
    }
    .info_right {
      flex: 1;
      height: 100%;
    }
  }
  .info_detail_title {
    height: 60px;
    line-height: 60px;
    font-weight: bold;
    font-size: 20px;
    padding-left: 20px;
    text-align: center;
    margin-top: 10px;
  }
  .table_con {
    margin: 0 20px;
    height: calc(100% - 100px);
  }
}
</style>
